#!/bin/sh

alias curl='curl -stderr'
alias echo='echo -e'


>tmp$$
echo "US Market======================================="
export Dow=`curl "http://money.cnn.com/data/us_markets/" |sed 's/<tr/\n/g' |grep -w ">Dow<" |egrep -o "[+|-][0-9]+.[0-9]+[<|%<]" |sed 's/<//g'`
export Nasdaq=`curl "http://money.cnn.com/data/us_markets/" |sed 's/<tr/\n/g' |grep -w ">NASDAQ<" |egrep -o "[+|-][0-9]+.[0-9]+[<|%<]" |sed 's/<//g'`
export SP=`curl "http://money.cnn.com/data/us_markets/" |sed 's/<tr/\n/g' |grep -w ">S&P<" |egrep -o "[+|-][0-9]+.[0-9]+[<|%<]" |sed 's/<//g'`
echo "Dow\t\t\t="$Dow
echo "Nasdaq\t\t\t="$Nasdaq
echo "S&P\t\t\t="$SP
curl "http://www.nasdaq.com/" |egrep  '#Exceeded|#Met|#Failed' |egrep -o ">[0-9]+<" |sed -e 's/>//g' -e 's/<//g' |tr '\n' ' ' |awk '{print "Earning Suprise:\tExceed="$1" Met="$2" Fail="$3}'
export newhighs=`curl "http://stockcharts.com/def/servlet/SC.scan" |grep Highs -A 6 |egrep -o ">[0-9]+<" |sed -e 's/>//g' -e 's/<//g' |tail -n 1`
export newlows=`curl "http://stockcharts.com/def/servlet/SC.scan" |grep Lows -A 6 |egrep -o ">[0-9]+<" |sed -e 's/>//g' -e 's/<//g' |tail -n 1`
echo "# New Highs\t\t="$newhighs
echo "# New low\t\t="$newlows

#pre-Market at Frankfurt
echo "World Market======================================"
export up=`curl "http://money.cnn.com/data/premarket/frankfurt/?" |egrep -o '\+[0-9.]+%' |wc -l`
export down=`curl "http://money.cnn.com/data/premarket/frankfurt/?" |egrep -o '\-[0-9.]+%' |wc -l`
echo "Frankfurt Up/Down\t="$up/$down
curl "http://money.cnn.com/data/premarket/" |sed 's/<tr/\n/g'|grep NIKKEI |egrep -o "[\+|\-][0-9.]+%" |awk '{print"NIKKEI\t\t\t="$1}'
curl "http://money.cnn.com/data/premarket/" |sed 's/<tr/\n/g'|grep HANG   |egrep -o "[\+|\-][0-9.]+%" |awk '{print"HANG SENG\t\t="$1}'
curl "http://money.cnn.com/data/premarket/" |sed 's/<tr/\n/g'|grep Euro   |egrep -o "[\+|\-][0-9.]+%" |awk '{print"$ vs. Euro\t\t="$1}'
curl "http://money.cnn.com/data/premarket/" |sed 's/<tr/\n/g'|grep Yen    |egrep -o "[\+|\-][0-9.]+%" |awk '{print"$ vs. Yen\t\t="$1}'
curl "http://money.cnn.com/data/premarket/" |sed 's/<tr/\n/g'|grep UK     |egrep -o "[\+|\-][0-9.]+%" |awk '{print"$ vs. UK\t\t="$1}'

echo "Commodity and Bonds================================"
oilgold=`curl "http://money.cnn.com/data/premarket/index.html" |egrep "Gold" |egrep -o "class=last>[0-9.]+" |cut -d'>' -f2 |tr '\n' ' '|awk '{print $1" "$2}'`
export bond=`curl "http://money.cnn.com/markets/bondcenter/index.html" |sed 's/<tr/\n/g' |egrep "[2|5|10|30] yr" |egrep -o "[>|>-][0-9.]+</span"  |cut -d'<' -f1 |tr '\n' ' '`
echo "Gold/Oil Price\t\t="$oilgold
echo "2/5/10/30 yr Bond change="$bond

#VIX
echo "Sentiment & Indicators=============================="
#./quote.sh ^VIX|cut -d':' -f2|awk '{printf("VIX\t\t\t=%s(pessemestic if 30+,optimistic if 20-)\n",$1)}'
#TradingMarket 5% rule
# Do not buy stocks (or the market) anytime the VIX is 5% below its moving average.
# Do not sell stocks (or the market) anytime the VIX is 5% above its moving average.
#http://www.tradingmarkets.com/.site/stocks/general/tmtradingrules/07282005-44482.cfm
vix10days=`curl "http://finance.yahoo.com/q/hp?s=%5EVIX" |sed 's/tr/\n/g' |grep yfnc_tabledata1 |egrep -o ">[0-9.]+</td></$" |sed -e 's/>//g' -e 's/<\/td<\///g' |head -n 10|tr '\n' '+' |sed 's/+$/\n/g'`
vix10ma=`echo "scale=2; ($vix10days)/10" |bc`
currentvix=`./quote.sh ^VIX |cut -d':' -f2 |awk '{print $1}'`
echo "$currentvix $vix10ma" |awk '{printf("VIX vs. its 10-day-MA\t=%2.2f,%1.2f(<0.95 overbought >1.05 oversold)\n",$1,$1/$2)}'

export putcallratio=`curl "http://www.cboe.com/data/mktstat.aspx"  |grep "Equity Put/Call Ratio" -A 1 |tail -n 1 |egrep -o ">[0-9]+.[0-9]+<" |sed -e 's/>//g' -e 's/<//g'` #use Equity-only P/C ration to avoid index put hedge noise
export advancedeclineratio=`curl "http://finance.yahoo.com/advances" |egrep -o "\([0-9]+%\)" |tr '\n' ' ' |sed -e 's/(//g' -e 's/)//g' -e 's/%//g' |awk '{printf("%1.2f\n",($1+$2+$3)/($5+$6+$7))}'`
echo "Put Call Ratio\t\t="$putcallratio"\t(Contrarian Sentiment: Near top if <0.6, Near Bottom if >1.2)"
echo "Advance/Decline Ratio\t="$advancedeclineratio"\t(Given market steady, Bearish if <0.5, Bullish if > 2.0))"

BullishPercentIndex=`curl "http://stockcharts.com/charts/NYSEBPI.html" |egrep -o "Count:[[:space:]]+</b>[ 0-9]+" |cut -d'>' -f2 |tr '\n' ' ' |awk '{printf("%1.2f",$1/$2)}'`
echo "Bullish Percentage Index="$BullishPercentIndex "\t(<30% oversold, >70% overbought)"
nyseadvancing=`curl -stderr "http://finance.yahoo.com/advances" |grep "Advancing Issues" |egrep -o ">[0-9,]+" |sed -e 's/>//g' -e 's/,//g'`
nysedeclining=`curl -stderr "http://finance.yahoo.com/advances" |grep "Declining Issues" |egrep -o ">[0-9,]+" |sed -e 's/>//g' -e 's/,//g'`
nyseupvol=`curl -stderr "http://finance.yahoo.com/advances" |grep "Up Volume" |egrep -o ">[0-9,]+$" |sed -e 's/>//g' -e 's/,//g'`
nysedownvol=`curl -stderr "http://finance.yahoo.com/advances" |grep "Down Volume" |egrep -o ">[0-9,]+$" |sed -e 's/>//g' -e 's/,//g'`
echo $nyseadvancing $nysedeclining $nyseupvol $nysedownvol |
awk '{printf("ARMs Index\t\t=%1.2f\t(<0.8 overbought,>1.2 oversold)\n",($1/$2)/($3/$4))}'

gmi=`curl -stderr 'http://wishingwealth.typepad.com/' |egrep  -o '>GMI.+<' |head -n 1 |cut -d'>' -f2 |cut -d'<' -f1`
echo -e "Dr. Wish Indicator\t="$gmi"(0:Bearish 6:Bullish)"

#Bullish/Bearish Crossover #
>tmp1$$
find .  -name "*Bullish_50n200-day_MA_Crossovers*" |sort -r |head -n 20 |while read line
do
    cat $line CompositeStocks |sort |uniq -d |wc -l >>tmp1$$
done
>tmp2$$
find .  -name "*Bearish_50n200-day_MA_Crossovers*" |sort -r |head -n 20 |while read line
do
    cat $line CompositeStocks |sort |uniq -d |wc -l >>tmp2$$
done

bullseq=`cat tmp1$$ |tr '\n' " "`
bearseq=`cat tmp2$$ |tr '\n' " "`
echo "Bullish Crossover:="$bullseq
echo "Bearish Crossover:="$bearseq
#try to get http://www.market-harmonics.com/tech_chart_descriptions.htm
cat tmpsp500 |awk '{print $3}' |tr '\n' '+' |sed -e 's/+$/\n/g' -e 's/Volume%+//g' > tmp$$
vol=`cat tmp$$ |bc`
num=`wc -l tmp$$`
avgvol=`echo "scale=2; $vol/$num "|bc`
echo "SP500 intraday volume\t="$avgvol

